package com.neuedu.io;

import java.io.*;

/**
 * @author 金山
 * 项目：javase
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2024/10/319:39
 */
public class MyFileReader {

    public static void main(String[] args) {


        readOneChar();


        readManyChar();


    }



    public static void  readManyChar(){
        File  file = new File("D:\\upload\\chinese.txt");


        //try  with 语法  1.7+ 不用手动 close
        try(Reader reader = new FileReader(file)) {
            char data[] = new char[1024];
            int len = -1;
            //读取一个字符
           while(   (len = reader.read(data))  != -1){

               String msg = new String(data,0,len);
               System.out.println("len = " + len);
               System.out.println("msg = " + msg);

           }
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    public static void  readOneChar(){
        File  file = new File("D:\\upload\\chinese.txt");


        //try  with 语法  1.7+ 不用手动 close
        // try(InputStream reader = new FileInputStream(file)) {    //低级的字节流， 对于中文 需要双字节
        try(Reader reader = new FileReader(file)) {

            //读取一个字符
            char ch = (char)reader.read();
            System.out.println("ch = " + ch);

            ch = (char)reader.read();
            System.out.println("ch = " + ch);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }



}
